package com.xf;

import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

public class Program {
    public static Map<String, Object> proxyMap = new HashMap<>();
    public static void prepare(String RigistAddress,Integer port){
        MyInvocationHandler handler = new MyInvocationHandler(RigistAddress,port,"1.0");//接口(Interface)版本号
        HelloService proxyInstance1 = (HelloService) java.lang.reflect.Proxy.newProxyInstance(
                HelloService.class.getClassLoader(),
                new Class<?>[] { HelloService.class },
                handler
        );

        handler = new MyInvocationHandler(RigistAddress,port,"2.0");
        HelloService proxyInstance3 = (HelloService) java.lang.reflect.Proxy.newProxyInstance(
                HelloService.class.getClassLoader(),
                new Class<?>[] { HelloService.class },
                handler
        );

        handler = new MyInvocationHandler(RigistAddress,port,"1.0");
        Arithmetic proxyInstance2 = (Arithmetic) Proxy.newProxyInstance(
                Arithmetic.class.getClassLoader(),
                new Class<?>[] { Arithmetic.class },
                handler
        );

        handler = new MyInvocationHandler(RigistAddress,port,"1.0");
        StringMethod proxyInstance4 = (StringMethod) Proxy.newProxyInstance(
                Arithmetic.class.getClassLoader(),
                new Class<?>[] { StringMethod.class },
                handler
        );

        proxyMap.put("StringMethod1.0",proxyInstance4);
        proxyMap.put("HelloService2.0",proxyInstance3);
        proxyMap.put("HelloService1.0",proxyInstance1);
        proxyMap.put("Arithmetic1.0",proxyInstance2);
    }
    public static void start() {
        //编写你的程序部分,相当于main函数
        Arithmetic arithmetic = (Arithmetic) proxyMap.get("Arithmetic1.0");
        String result=arithmetic.add(1.0,2.0).toString();
        System.out.println(result);
        System.out.println(arithmetic.mul(1.0,2.0).toString());
        System.out.println(arithmetic.div(1.0,2.0).toString());
        System.out.println(arithmetic.sub(1.0,2.0).toString());

        HelloService helloService = (HelloService) proxyMap.get("HelloService1.0");
        System.out.println(helloService.sayingHello("xf23232").toString());

        helloService = (HelloService) proxyMap.get("HelloService2.0");
        System.out.println(helloService.sayingHello("xf23232").toString());

        StringMethod stringMethod = (StringMethod)proxyMap.get("StringMethod1.0");
        String s="absdiioasssa";
        System.out.println(stringMethod.count_num(s,'s'));
        System.out.println(stringMethod.turn_up(s));
        System.out.println(stringMethod.turn_low(s));
        System.out.println(stringMethod.reversal(s));
    }
}
